<template>
  <div>
    <div class="btn">
      <el-button style="float: right" @click="addEduView">添加地址</el-button>
    </div>
    <DistrictAssembly
      v-for="(item, index) in edus"
      :key="index"
      :index="index"
      :item="item"
      @deleteIndex="deleteEdusTion"
      ref="rulesDistrict"
    ></DistrictAssembly>
    <el-button @click="onSubmit">提交</el-button>
  </div>
</template>
<script>
import DistrictAssembly from "./DistrictAssembly";
import { putUpDistrict, getAddress } from "../../assets/js/api";
export default {
  name: "DistrictDia",
  components: { DistrictAssembly },
  props: ["orderNumber"],
  data() {
    return {
      edus: [],
      form: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取原有地区
    getList() {
      getAddress(this.orderNumber).then(res => {
        if (res.code == 200) {
          if (res.data.length > 0) {
            res.data.map(item => {
              this.edus.push(item);
            });
          } else {
            let obj = {};
            obj.province = "";
            obj.city = "";
            obj.area = "";
            this.edus.push(obj);
          }
        } else {
          let obj = {};
          obj.province = "";
          obj.city = "";
          obj.area = "";
          this.edus.push(obj);
        }
      });
    },
    // 添加地区选择
    addEduView() {
      this.edus.push({});
    },
    // 移除地区选择
    deleteEdusTion(index) {
      if (index !== 0) {
        this.edus.splice(index, 1);
      }
    },
    // 提交
    onSubmit() {
      if (this.rulesFun() == true) {
        this.form.orderNumber = this.orderNumber;
        this.form.ids = this.edus;
        putUpDistrict(this.form).then(res => {
          if (res.code == 200) {
            this.$emit("closeDia", false);
            this.edus = [{}];
            this.$message.success(res.message);
          } else {
            this.$message.error(res.message);
          }
        });
      }
    },
    rulesFun() {
      let flag = false;
      this.edus.map(item => {
        for (let index in item) {
          if (item[index] == "") {
            this.$message.warning("请完成选项");
            flag = false;
          } else {
            flag = true;
          }
        }
        return;
      });
      return flag;
    },
    DistrictFormFun() {
      this.edus = [{}];
    }
  }
};
</script>
<style scoped type="less">
.btn {
  width: 100%;
  height: 50px;
}
</style>
